package org.itheima.googleplay.factory;

import java.util.LinkedHashMap;
import java.util.Map;

import org.itheima.googleplay.ui.fragment.AppFragment;
import org.itheima.googleplay.ui.fragment.BaseFragment;
import org.itheima.googleplay.ui.fragment.CategoryFragment;
import org.itheima.googleplay.ui.fragment.GameFragment;
import org.itheima.googleplay.ui.fragment.HomeFragment;
import org.itheima.googleplay.ui.fragment.HotFragment;
import org.itheima.googleplay.ui.fragment.RecommendFragment;
import org.itheima.googleplay.ui.fragment.SubjectFragment;
import org.itheima.googleplay.utils.LogUtils;

/**
 * @包名: org.itheima.googleplay.factory
 * @类名: PagerFactory
 * @作者: 肖琦
 * @创建时间 : 2015-3-20 下午2:19:20
 * 
 * @描述: 提供主页中对应的子页面的工厂类
 * 
 * @SVN版本: $Rev: 18 $
 * @更新人: $Author: xq $
 * @更新时间: $Date: 2015-03-23 16:00:13 +0800 (星期一, 23 三月 2015) $
 * 
 * @更新描述:
 */
public class PagerFactory
{
	private static Map<Integer, BaseFragment>	mCaches	= new LinkedHashMap<Integer, BaseFragment>();

	public static BaseFragment getFragment(int position)
	{

		// <item>首页</item>
		// <item>应用</item>
		// <item>游戏</item>
		// <item>专题</item>
		// <item>推荐</item>
		// <item>分类</item>
		// <item>排行</item>
		BaseFragment fragment = mCaches.get(position);

		// 判断缓存中是否有
		if (fragment != null)
		{
			LogUtils.d("读取缓存 : " + position);
			return fragment;
		}

		switch (position)
		{
			case 0:
				// 首页
				fragment = new HomeFragment();
				break;
			case 1:
				// 应用
				fragment = new AppFragment();
				break;
			case 2:
				// 游戏
				fragment = new GameFragment();
				break;
			case 3:
				// 专题
				fragment = new SubjectFragment();
				break;
			case 4:
				// 推荐页面
				fragment = new RecommendFragment();
				break;
			case 5:
				// 分类
				fragment = new CategoryFragment();
				break;
			case 6:
				// 排行
				fragment = new HotFragment();
				break;
			default:
				fragment = new HomeFragment();
				break;
		}

		// 存储到缓存
		mCaches.put(position, fragment);

		LogUtils.d("新建缓存 : " + position);

		return fragment;
	}
}
